package com.ruoyi.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.IdVo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.UserPropertyVo;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.user.domain.TbUser;
import com.ruoyi.user.domain.TbUserProperty;
import com.ruoyi.user.mapper.TbUserMapper;
import com.ruoyi.user.service.TbUserPropertyService;
import com.ruoyi.user.mapper.TbUserPropertyMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class TbUserPropertyServiceImpl extends ServiceImpl<TbUserPropertyMapper, TbUserProperty>
        implements TbUserPropertyService {

    @Autowired
    private TbUserPropertyMapper tbUserPropertyMapper;

    @Autowired
    private TbUserMapper tbUserMapper;

    @Override
    public R addUserProperty(UserPropertyVo userPropertyVo) {

        TbUser tbUser = tbUserMapper.selectById(userPropertyVo.getUserId());

        if (Objects.isNull(tbUser))
            return R.fail("添加资产用户不存在");

        TbUserProperty tbUserProperty = new TbUserProperty();
        BeanUtils.copyProperties(userPropertyVo, tbUserProperty);
        tbUserProperty.setCreateBy(tbUser.getUserName());
        tbUserProperty.setUpdateBy(tbUser.getUserName());

        int insert = tbUserPropertyMapper.insert(tbUserProperty);

        return insert > 0 ? R.ok("添加成功") : R.fail("持久层错误,添加失败");
    }

    @Override
    public AjaxResult selectUserPropertyByUserId(IdVo idVo) {

        List<TbUserProperty> tbUserProperties = tbUserPropertyMapper.selectList(
                new LambdaQueryWrapper<TbUserProperty>().eq(TbUserProperty::getUserId, idVo.getId())
        );

        List<UserPropertyVo> userPropertyVoList = tbUserProperties.stream().map(tbUserProperty -> {
            UserPropertyVo userPropertyVo = new UserPropertyVo();
            BeanUtils.copyProperties(tbUserProperty, userPropertyVo);
            return userPropertyVo;
        }).collect(Collectors.toList());

        return AjaxResult.success(userPropertyVoList);
    }

    @Override
    @Transactional
    public R deleteUserPropertyById(IdVo idVo) {

        TbUserProperty tbUserProperty = tbUserPropertyMapper.selectById(idVo.getId());
        if (Objects.isNull(tbUserProperty))
            return R.fail("删除的资产不存在");

        TbUser tbUser = tbUserMapper.selectById(tbUserProperty.getUserId());
        if (Objects.isNull(tbUser))
            return R.fail("资产所有用户不存在,无法删除");

        tbUserProperty.setUpdateBy(tbUser.getUserName());
        tbUserProperty.setUpdateTime(null);
        tbUserPropertyMapper.updateById(tbUserProperty);

        int delete = tbUserPropertyMapper.deleteById(idVo.getId());

        return delete > 0 ? R.ok("删除成功") : R.fail("删除失败");
    }

    @Override
    public R updateOfUserProperty(UserPropertyVo userPropertyVo) {

        TbUserProperty tbUserProperty = tbUserPropertyMapper.selectById(userPropertyVo.getUserPropertyId());
        if (Objects.isNull(tbUserProperty))
            return R.fail("修改的资产不存在");

        TbUser tbUser = tbUserMapper.selectById(tbUserProperty.getUserId());
        if (Objects.isNull(tbUser))
            return R.fail("资产所有用户不存在,无法修改");

        tbUserProperty.setUserPropertyPic(userPropertyVo.getUserPropertyPic());
        tbUserProperty.setUserPropertyName(userPropertyVo.getUserPropertyName());
        tbUserProperty.setUpdateBy(tbUser.getUserName());
        tbUserProperty.setUpdateTime(null);

        int update = tbUserPropertyMapper.updateById(tbUserProperty);
        return update > 0 ? R.ok("修改成功") : R.fail("修改失败");
    }


}




